package com.zhupanlin.service;

import com.zhupanlin.dao.UserDao;
import com.zhupanlin.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 描述信息：
 *
 * @author zhupanlin
 * @version 1.0
 * @date 2024/12/23 11:42
 */
@Service
@RequiredArgsConstructor
public class UserService implements UserDetailsService {

    private final UserDao userDao;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.根据用户名查询用户信息
        User user = userDao.loadUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(username + " not found");
        }
        // 2.查询用户的角色信息
        user.setRoles(userDao.getRolesByUid(user.getId()));
        return user;
    }
}
